package com.collect.tanzania.zhiScene.zhiWidget.zhiFilterPerformance;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiFilterPerformance.ZHIFilterLevelAdjuster;
import com.collect.tanzania.zhiAdater.zhiFilterPerformance.ZHIFilterMonthAdjuster;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRFilterEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRGroupUserEntity;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterAdminGroup.ZHIRangePicker;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDateSelect;
import com.collect.tanzania.zhiScene.zhiWidget.zhiFilterDaily.ZHIRangeDay;
import com.collect.tanzania.zhiUtils.StringUtil;
import com.collect.tanzania.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;

public class ZHIFilterPerformance extends RelativeLayout {

    public static final String TYPE_MONEY = "MONEY";
    public static final String TYPE_ORDER = "ORDER";

    private final Context mContext;

    private String mDataType = TYPE_MONEY;

    private String mLevel;
    private String mMonth;

    private ZHIRangeDateSelect rds_due_date;
    private ZHIRangePicker rds_member;

    private ZHIFilterLevelAdjuster mZHIFilterLevelAdjuster;
    private ZHIFilterMonthAdjuster mZHIFilterMonthAdjuster;

    private ArrayList<ZHIRFilterEntity> mLevelFilterArrayList;
    private ArrayList<ZHIRFilterEntity> mMonthFilterArrayList;

    private ZHIFilterPerformanceClickListener mZHIFilterPerformanceClickListener;

    public ZHIFilterPerformance(Context context) {
        this(context, null);
    }

    public ZHIFilterPerformance(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ZHIFilterPerformance(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.mContext = context;

        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ZHIFilterPerformance);
        mDataType = ta.getString(R.styleable.ZHIFilterPerformance_dataType);
        ta.recycle();

        initView();
    }

    private void initView() {
        mMonth = ZHICalendarUtil.getMonth() + "";
        mLevel = ZHIMainProcessHelper.getInstance().getLevel();

        View mView = View.inflate(mContext, R.layout.view_filter_performance, this);
        rds_due_date = mView.findViewById(R.id.rds_due_date);
        rds_member = mView.findViewById(R.id.rds_member);

        TextView tv_reset = mView.findViewById(R.id.tv_reset);
        tv_reset.setOnClickListener(v -> {
            for (ZHIRFilterEntity zhirFilterEntity1 : mLevelFilterArrayList) {
                zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().contains(ZHIMainProcessHelper.getInstance().getLevel()));
            }
            mLevel = ZHIMainProcessHelper.getInstance().getLevel();
            mZHIFilterLevelAdjuster.notifyDataSetChanged();

            String curMonth = ZHICalendarUtil.getMonthSimpleEng();
            for (ZHIRFilterEntity zhirFilterEntity1 : mMonthFilterArrayList) {
                zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equals(curMonth));
            }
            mMonth = ZHICalendarUtil.getMonth() + "";
            mZHIFilterMonthAdjuster.notifyDataSetChanged();

            rds_due_date.reset();
            rds_member.reset();
        });

        RelativeLayout rl_close_bg = mView.findViewById(R.id.rl_close_bg);
        rl_close_bg.setOnClickListener(v -> {
            if (mZHIFilterPerformanceClickListener != null) {
                mZHIFilterPerformanceClickListener.onCloseClick();
            }
        });

        TextView tv_submit = mView.findViewById(R.id.tv_submit);
        tv_submit.setOnClickListener(v -> {
            if (mZHIFilterPerformanceClickListener != null) {
                mZHIFilterPerformanceClickListener.onConfirmClick();
            }
        });

        RecyclerView rc_level_list = mView.findViewById(R.id.rc_level_list);
        if (mDataType.equalsIgnoreCase(TYPE_ORDER)) {
            initLevelOrderDataSource();
        } else {
            initLevelMoneyDataSource();
        }

        mZHIFilterLevelAdjuster = new ZHIFilterLevelAdjuster();
        mZHIFilterLevelAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
            if (zhirFilterEntity != null) {
                mLevel = zhirFilterEntity.getLevel();
                for (ZHIRFilterEntity zhirFilterEntity1 : mLevelFilterArrayList) {
                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equals(zhirFilterEntity.getLevel()));
                }
                mZHIFilterLevelAdjuster.notifyDataSetChanged();
            }
        });
        rc_level_list.setLayoutManager(new GridLayoutManager(mContext, 3));
        rc_level_list.setAdapter(mZHIFilterLevelAdjuster);
        mZHIFilterLevelAdjuster.setNewData(mLevelFilterArrayList);

        ///////////////////////
        //暂时隐藏--start
//        RecyclerView rc_month_list = mView.findViewById(R.id.rc_month_list);
//        String[] months = new String[]{
//                "Jan", "Feb", "Mar", "Apr",
//                "May", "Jun", "July", "Aug",
//                "Sep", "Oct", "Nov", "Dec"};
//        String curMonth = ZHICalendarUtil.getMonthSimpleEng();
//        mMonthFilterArrayList = new ArrayList<>();
//        for (int i = 0; i < months.length; i++) {
//            ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
//            zhirFilterEntity.setLevel(months[i]);
//            zhirFilterEntity.setChecked(months[i].equalsIgnoreCase(curMonth));
//            mMonthFilterArrayList.add(zhirFilterEntity);
//        }
//        mZHIFilterMonthAdjuster = new ZHIFilterMonthAdjuster();
//        mZHIFilterMonthAdjuster.setOnZHIFilterAdjusterItemClickListener((zhirFilterEntity, position) -> {
//            if (zhirFilterEntity != null) {
//                mMonth = (position + 1) + "";
//                for (ZHIRFilterEntity zhirFilterEntity1 : mMonthFilterArrayList) {
//                    zhirFilterEntity1.setChecked(zhirFilterEntity1.getLevel().equalsIgnoreCase(zhirFilterEntity.getLevel()));
//                }
//                mZHIFilterMonthAdjuster.notifyDataSetChanged();
//            }
//        });
//        rc_month_list.setLayoutManager(new GridLayoutManager(mContext, 4));
//        rc_month_list.setAdapter(mZHIFilterMonthAdjuster);
//        mZHIFilterMonthAdjuster.setNewData(mMonthFilterArrayList);
        //暂时隐藏--end
        ///////////////////////

        ////////////////////////////////////
        initDueDateDataSource();

        ////////////////////////////////////
        rds_member.setDataSource(initUserDataSource());
    }

    public ArrayList<ZHIRangeDay> initUserDataSource() {
        ArrayList<ZHIRangeDay> arrayList = new ArrayList<>();
        try {
//            ArrayList<String> userList = CFMainProcessHelper.getInstance().getLoginEntity().getGroupUserLoginNameList();
//            for (int i = 0; i < userList.size(); i++) {
//                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
//                zhiRangeDay.setRangeId(i + "");
//                zhiRangeDay.setDayTitle(userList.get(i));
//                arrayList.add(zhiRangeDay);
//            }

            ArrayList<ZHIRGroupUserEntity> userList = ZHIMainProcessHelper.getInstance().getGroupUserEntityArrayList();
            for (int i = 0; i < userList.size(); i++) {
                ZHIRGroupUserEntity zhirGroupUserEntity = userList.get(i);
                ZHIRangeDay zhiRangeDay = new ZHIRangeDay();
                zhiRangeDay.setRangeId(i + "");
                zhiRangeDay.setDayTitle(zhirGroupUserEntity.getLoginName());
                arrayList.add(zhiRangeDay);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return arrayList;
    }

    private void initDueDateDataSource() {
        try {
            int month = ZHICalendarUtil.getMonth();
            int cday = ZHICalendarUtil.getDay();

            String monthString = month >= 10 ? String.valueOf(month) : ("0" + month);
            String cdayString = cday >= 10 ? String.valueOf(cday) : ("0" + cday);

            String startFormatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + "01";
            rds_due_date.updatePromisedDueTimeStart(false, ZHICalendarUtil.getDateFromFormatString(startFormatString));

            String endFormatString = ZHICalendarUtil.getYear() + "-" + monthString + "-" + cdayString;
            rds_due_date.updatePromisedDueTimeEnd(false, ZHICalendarUtil.getDateFromFormatString(endFormatString));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initLevelMoneyDataSource() {
        try {
            mDataType = TYPE_MONEY;

            if (TextUtils.isEmpty(mLevel)) {
                mLevel = ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLeven();
            }

            String[] levels = new String[]{"B1", "B2", "B3+"};

            if (!TextUtils.isEmpty(mLevel)) {
                boolean hadExist = false;

                for (String glevel : levels) {
                    if (mLevel.equalsIgnoreCase(glevel)) {
                        hadExist = true;
                        break;
                    }
                }

                if(!hadExist){
                    mLevel = "";
                }
            }

            if (mLevelFilterArrayList != null) {
                mLevelFilterArrayList.clear();
            } else {
                mLevelFilterArrayList = new ArrayList<>();
            }

            for (String level : levels) {
                ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
                zhirFilterEntity.setLevel(level);
                zhirFilterEntity.setChecked(level.contains(mLevel));
                mLevelFilterArrayList.add(zhirFilterEntity);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initLevelOrderDataSource() {
        try {
            mDataType = TYPE_ORDER;

            if (TextUtils.isEmpty(mLevel)) {
                mLevel = ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLeven();
            }

            String[] levels = new String[]{"B1", "B2", "B3", "B4", "B5"};

            if (!TextUtils.isEmpty(mLevel)) {
                boolean hadExist = false;

                for (String glevel : levels) {
                    if (mLevel.equalsIgnoreCase(glevel)) {
                        hadExist = true;
                        break;
                    }
                }

                if(!hadExist){
                    mLevel = "";
                }
            }

            if (mLevelFilterArrayList != null) {
                mLevelFilterArrayList.clear();
            } else {
                mLevelFilterArrayList = new ArrayList<>();
            }

            for (String level : levels) {
                ZHIRFilterEntity zhirFilterEntity = new ZHIRFilterEntity();
                zhirFilterEntity.setLevel(level);
                zhirFilterEntity.setChecked(level.contains(mLevel));
                mLevelFilterArrayList.add(zhirFilterEntity);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setToTypeMoney() {
        initLevelMoneyDataSource();
        if (mZHIFilterLevelAdjuster != null) {
            mZHIFilterLevelAdjuster.setNewData(mLevelFilterArrayList);
        }
    }

    public void setToTypeOrder() {
        initLevelOrderDataSource();
        if (mZHIFilterLevelAdjuster != null) {
            mZHIFilterLevelAdjuster.setNewData(mLevelFilterArrayList);
        }
    }

    public void updateUserDataSource() {
        rds_member.setDataSource(initUserDataSource());
    }

    public String getLevel() {
        try {
            String tempLevel = StringUtil.safeString(mLevel);
            if (mDataType.equalsIgnoreCase(TYPE_ORDER)) {
                //order需要转M
                tempLevel = tempLevel.replaceAll("\\+", "");
                tempLevel = tempLevel.replaceAll("B", "M");

            } else {
                //money 不需要转M，但需要去3
                if (tempLevel.contains("B3+")) {
                    tempLevel = tempLevel.replaceAll("\\+", "");
                }
            }
            return tempLevel;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "M1";
    }

    public String getMonth() {
        return StringUtil.safeString(mMonth);
    }

    public String getDudStartDate() {
        return StringUtil.safeString(rds_due_date.getStartDate());
    }

    public String getDueEndDate() {
        return StringUtil.safeString(rds_due_date.getEndDate());
    }

    public String getMember() {
        String user = rds_member.getCompare();
        try {
            if (ZHIMainProcessHelper.getInstance().getGroupUserEntityArrayList() != null && ZHIMainProcessHelper.getInstance().getGroupUserEntityArrayList().size() > 0) {
                for (int i = 0; i < ZHIMainProcessHelper.getInstance().getGroupUserEntityArrayList().size(); i++) {
                    ZHIRGroupUserEntity zhirGroupUserEntity = ZHIMainProcessHelper.getInstance().getGroupUserEntityArrayList().get(i);
                    if (zhirGroupUserEntity.getLoginName().equalsIgnoreCase(user)) {
                        user = zhirGroupUserEntity.getUserId();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return user;
    }

    public void setZHIPerformanceFilterClickListener(ZHIFilterPerformanceClickListener clickListener) {
        this.mZHIFilterPerformanceClickListener = clickListener;
    }
}
